package com.telecom.weixi.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.telecom.weixi.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class GridAdapter extends BaseAdapter {
	List<Map<String, Object>> mGridData;
	Context mContext;
	
	public static final String TAG_MENU_NAME 	= "menu_name"; 	//String
	public static final String TAG_MENU_ICON 	= "menu_icon";		//drawable
	public static final String TAG_TEXT_COLOR 	= "text_color";		//int
	public GridAdapter(Context context){
		mGridData = new ArrayList<Map<String,Object>>();
		mContext = context;
	}
	
	public void addItem(Map<String, Object> item){
		mGridData.add(item);
	}
	
	public void clearData(){
		mGridData.clear();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mGridData.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mGridData.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		TextView item;
		if(convertView == null){
			item = new TextView(mContext);
//			item.setPadding(0, 3, 0, 3);
		}else{
			item = (TextView) convertView;
		}
		
		Map<String, Object> itemData = mGridData.get(position);
		String name = (String) itemData.get(TAG_MENU_NAME);
		int color = (Integer) itemData.get(TAG_TEXT_COLOR);
		Drawable icon = (Drawable) itemData.get(TAG_MENU_ICON);
		
		item.setText(name);
		if(color == -1){
			item.setTextColor(mContext.getResources().getColor(R.color.menu_item_color));
		}else{
			item.setTextColor(color);
		}
		item.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
		item.setGravity(Gravity.CENTER_HORIZONTAL);
		item.setCompoundDrawablePadding(3);
		return item;
		
	}

}
